﻿#region License

// Created by John Batte on 04/04/2012 at 10:53 AM.
// Last code cleanup: 04/04/2012 at 10:53 AM.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Linq;
using System.Text;

namespace BatteCode.Google.Wiki
{
	/// <summary>
	/// Provides reflection-based extensions.
	/// </summary>
	public static class ReflectionExtensions
	{
		/// <summary>
		/// Gets the friendly name of the type.
		/// </summary>
		/// <param name="type">The type.</param>
		/// <returns></returns>
		public static string GetFriendlyName(this Type type)
		{
			if (type.IsGenericParameter)
				return type.Name;

			if (!type.IsGenericType)
				return type.FullName;

			var builder = new StringBuilder();
			string name = type.Name;
			int index = name.IndexOf("`", StringComparison.Ordinal);
			builder.AppendFormat("{0}.{1}", type.Namespace, name.Substring(0, index));
			string typeArgs = string.Join(",", type.GetGenericArguments().Select(x => x.GetFriendlyName()).ToArray());
			builder.AppendFormat("<{0}>", typeArgs);
			return builder.ToString();
		}
	}
}